<?php

require_once('../util/DataBaseUtil.php');
require_once('../util/LogUtil.php');

require_once('../vo/Contact.php');
require_once('../vo/User.php');
require_once('../vo/request/DeleteContactRequest.php');
require_once('../vo/request/FindContactsByUserRequest.php');
require_once('../vo/request/SaveContactRequest.php');
require_once('../vo/response/DeleteContactResponse.php');
require_once('../vo/response/FindContactsByUserResponse.php');
require_once('../vo/response/SaveContactResponse.php');

/***************************/
/** FIND CONTACTS BY USER **/
/***************************/

function findContactsByUser($request)
{
  $response = new FindContactsByUserResponse();

  connect();

  $userContacts = array();
  $sql = "SELECT * FROM `contacts` WHERE `userId` = ".$request['userId']." ORDER BY `lastName` ASC, firstName ASC;";
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving contacts.";
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $c = new Contact();
    $c->init($r['id'], $r['userId'], $r['firstName'], $r['lastName'], $r['address'], $r['city'], $r['email'], $r['phone'], $r['groupId']);
    $userContacts[] = $c;
  }

  mysql_close();

  $response->userContacts = $userContacts;
  return $response;
}

/******************/
/** SAVE CONTACT **/
/******************/

function saveContact($request)
{
  $response = new SaveContactResponse();
  $dataContact = $request['contact'];

  $contact = new Contact();
  $contact->id = $dataContact['id'];
  $contact->userId = $dataContact['userId'];
  $contact->firstName = $dataContact['firstName'];
  $contact->lastName = $dataContact['lastName'];
  $contact->address = $dataContact['address'];
  $contact->city = $dataContact['city'];
  $contact->email = $dataContact['email'];
  $contact->phone = $dataContact['phone'];
  $contact->groupId = $dataContact['groupId'];

  connect();

  log_info("contact.id = ".$contact->id
          ."\ncontact.city = ".$contact->city
          ."\nuserId = ".$request['userId']);
  if ($contact->id == 0)
  {
    $query = "INSERT INTO `contacts` (userId, firstName, lastName, address, city, email, phone, groupId) "
            ."VALUES ('".$request['userId']."', '".$contact->firstName."', '".$contact->lastName."', '".$contact->address."', '".$contact->city."', '".$contact->email."', '".$contact->phone."', '".$contact->groupId."');";
  }
  else
  {
    $query = " UPDATE `contacts` SET "
           ." `id`='".$contact->id."', "
           ." `userId`='".$contact->userId."', "
           ." `firstName`='".$contact->firstName."', "
           ." `lastName`='".$contact->lastName."', "
           ." `address`='".$contact->address."', "
           ." `city`='".$contact->city."', "
           ." `email`='".$contact->email."', "
           ." `phone`='".$contact->phone."', "
           ." `groupId`='".$contact->groupId."' "
           ." WHERE `id`='".$contact->id."';";
  }

  log_info($query);
  mysql_query($query)
    or die ('Une erreur est survenue lors de l\'ajout.<br>'.mysql_error());

  $userContacts = array();
  $sql = "SELECT * FROM `contacts` WHERE `userId` = ".$request['userId']." ORDER BY `lastName` ASC, firstName ASC;";
  log_info("select final = ".$sql);
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving contacts.";
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $c = new Contact();
    $c->init($r['id'], $r['userId'], $r['firstName'], $r['lastName'], $r['address'], $r['city'], $r['email'], $r['phone'], $r['groupId']);
    $userContacts[] = $c;
  }

  mysql_close();

  $response->userContacts = $userContacts;
  return $response;
}

/********************/
/** DELETE CONTACT **/
/********************/

function deleteContact($request)
{
  $response = new DeleteContactResponse();
  $userId = $request['userId'];
  $contactId = $request['contactId'];

  log_info("contact.id = ".$contactId
          ."\nuserId = ".$userId);

  connect();

  $query = "DELETE FROM `contacts` WHERE `id`='".$contactId."';";
  log_info($query);
  mysql_query($query);

  $userContacts = array();
  $sql = "SELECT * FROM `contacts` WHERE `userId` = ".$userId." ORDER BY `lastName` ASC, firstName ASC;";
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving contacts.";
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $c = new Contact();
    $c->init($r['id'], $r['userId'], $r['firstName'], $r['lastName'], $r['address'],  $r['city'], $r['email'], $r['phone'], $r['groupId']);
    $userContacts[] = $c;
  }

  mysql_close();

  $response->userContacts = $userContacts;
  return $response;
}

?>
